<?php

namespace App\Recharge\Api;

use App\Models\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Yushang extends AbstractApi
{
    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {

        $array = [
            'AppKey'        => $bean->getSupplierNum(), //商户密钥
            'TimesTamp'     => millisecondTimestamp(),
            'ProductCode'   => $bean->getGoodsId(), //商品编码
            'BuyCount'      => $bean->getRechargeNum() * $bean->getStartCharging(),//充值数量,
            'MOrderID'      => $bean->getOrderNo(), //订单号
            'ChargeAccount' =>  $bean->getAccount(), //充值帐号
            'CustomerIP'    => $bean->getIp(),
            'CallBackUrl'   => $bean->getNotifyUrl(), //异步回调地址
        ];
        ksort($array);
        $sign = implode('', $array);
        $sign .= $bean -> getSecretKey();
        $array['Sign'] = strtoupper(md5($sign));
        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('state', '');
        $order_sn = $request->input('MOrderID', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['2', '3']))->setRechargeSuccess($status == '2');
    }

    /**
     * @return mixed
     */
    public function apiNotifySuccess(): mixed
    {
        return response(json_encode(['Code'=>0]));
    }
}
